package org.www.dao;

import java.util.List;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.www.bean.User;

public class UserDao {
	private HibernateTemplate hibernateTemplate;

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	public User findUserById(int id) {
		return hibernateTemplate.get(User.class, id) ;		
	}

	public List<User> findAll() {
		List<User> list=(List<User>) hibernateTemplate.find("from User");
		return list;		
	}
	public List findUserByName(String uname) {
		List list=hibernateTemplate.find("from User where uname=?",uname);
		return list;		
	}
	
	public void addUser(User user) {
		hibernateTemplate.save(user);
	}
	public void deleteUser(User user) {
		hibernateTemplate.delete(user);
	}
	public void updateUser(User user) {
		hibernateTemplate.saveOrUpdate(user);
	}

    public boolean login(String uname,String password) {
		List list =  hibernateTemplate.find
				("from User where uname = ? and password = ?", new Object[] {uname,password});
		if (list.size()>0) {
			return true;
		}
		return false;
	}

}
